package cn.b.sky.admin.utils;

import cn.b.sky.admin.enums.DwzCallbackType;
import cn.b.sky.admin.enums.DwzStatusCode;
import cn.b.sky.admin.model.DWZResult;

import javax.servlet.http.HttpServletRequest;


public class AjaxUtil {
  
  private AjaxUtil() {
    // Hide the constructor
  }
  
  /**
   * 
   * <strong>Title : isAjax<br>
   * </strong> <strong>Description : </strong>判断请求是否为ajax请求<br>
   * <strong>Create on : 2015-8-5 上午9:56:47<br>
   * </strong>
   * <p>
   * 
   * @param request
   * @return boolean
   * @throws
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  public static boolean isAjax(HttpServletRequest request) {
    if (request != null && "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
      return true;
    }
    return false;
  }

  public static DWZResult ajaxError(String message) {
    return ajaxError(message, null);
  }

  /**
   * 
   * <strong>Title : ajaxError<br></strong>
   * <strong>Description : </strong>新增详细错误提示信息<br> 
   * <strong>Create on : 2015-12-30 上午10:55:16<br></strong>
   * <p>
   * @param message
   * @param detailMessage
   * @return
   * DWZResult
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  public static DWZResult ajaxError(String message, String detailMessage) {
    DWZResult result = getDWZJsonObject(DwzStatusCode.ERROR, message, null, null, DwzCallbackType.EMPTY, null, null,
        null);
    result.setDetailMessage(detailMessage);
    return result;
  }

  public static DWZResult ajaxTimeout(String message) {
    return getDWZJsonObject(DwzStatusCode.TIMEOUT, message, null, null, DwzCallbackType.EMPTY, null, null, null);
  }

  public static DWZResult getDWZJsonObject(DwzStatusCode statusCode, String message, String navTabId, String rel,
      DwzCallbackType callbackType, String forwardUrl, String confirmMsg, Object data) {
    DWZResult dwzResult = null;

    if (null == statusCode) {
      return null;
    }
    if (null == callbackType) {// 回调状态
      callbackType = DwzCallbackType.EMPTY;
    }
    dwzResult = new DWZResult(statusCode.getValue(), message, navTabId, rel, callbackType.getValue(), forwardUrl, confirmMsg, data);
    return dwzResult;
  }
}
